package br.com.lucasddaniel.inbuzios.activities;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import br.com.lucasddaniel.inbuzios.Controller;
import br.com.lucasddaniel.inbuzios.R;
import br.com.lucasddaniel.inbuzios.bean.ResponsavelBean;
import br.com.lucasddaniel.inbuzios.dao.ExcessaoBanco;

public class CadastroResponsavelActivity extends Activity {

	private EditText m_edttext_login;
	private EditText m_edttext_senha;
	private EditText m_edttext_senha_confirm;
	private Button m_btn_cadastrar;
	private Activity activity = this;

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_cadastro_responsavel );
		initComponents();
	}

	private void initComponents() {
		m_edttext_login = ( EditText ) findViewById( R.id_activity_cadastro_responsavel.login );
		m_edttext_senha = ( EditText ) findViewById( R.id_activity_cadastro_responsavel.senha );
		m_edttext_senha_confirm = ( EditText ) findViewById( R.id_activity_cadastro_responsavel.senha_confirm );
		m_btn_cadastrar = ( Button ) findViewById( R.id_activity_cadastro_responsavel.btn_cadastrar );

		m_btn_cadastrar.setOnClickListener( new OnClickListener() {

			public void onClick( View v ) {
				String senha = m_edttext_senha.getText().toString();
				String senhaConfirm = m_edttext_senha_confirm.getText().toString();
				String login = m_edttext_login.getText().toString();
				Animation shake = AnimationUtils.loadAnimation( getApplicationContext(), R.anim.shake );
				if( TextUtils.isEmpty( senha ) || TextUtils.isEmpty( senhaConfirm ) || TextUtils.isEmpty( login ) ) {
					if( TextUtils.isEmpty( senha ) ) {
						m_edttext_senha.startAnimation( shake );
					}
					if( TextUtils.isEmpty( login ) ) {
						m_edttext_login.startAnimation( shake );
					}
					if( TextUtils.isEmpty( senhaConfirm ) ) {
						m_edttext_senha_confirm.startAnimation( shake );
					}
					Toast.makeText( getApplicationContext(), getResources().getString( R.string.activity_cadastro_responsavel_msgerror1 ), Toast.LENGTH_SHORT ).show();
				} else if( senha.equals( senhaConfirm ) ) {
					ResponsavelBean rb = new ResponsavelBean( login, senha );
					try {
						Controller.getInstance().insereResponsavel( rb, getApplicationContext() );
						activity.finish();
					} catch( ExcessaoBanco e ) {
						e.printStackTrace();
					}
				} else {

					m_edttext_senha_confirm.startAnimation( shake );
					m_edttext_senha.startAnimation( shake );
					Toast.makeText( getApplicationContext(), getResources().getString( R.string.activity_cadastro_responsavel_msgerror2 ), Toast.LENGTH_SHORT ).show();
				}

			}
		} );
	}
}
